package com.primaryedu.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.primaryedu.R;
import com.primaryedu.bean.CommentBean;
import com.primaryedu.picture.PicturePreviewActivity;
import com.primaryedu.widget.CircularImage;

/**
 * 评论适配器
 *
 * @author jun.wang
 * @description
 * @data 2016年12月19日
 */
public class CommentAdapter extends BaseAdapter {

    private Context context;
    LayoutInflater inflater;
    List<CommentBean> list = new ArrayList<>();
//    LoadImage loadImage;

    public CommentAdapter(Context context, List<CommentBean> list) {
        this.context = context;
        this.list = list;
//        loadImage = LoadImage.getInstance();
        inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (null == convertView) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_comment, null);
            viewHolder.headImage = (CircularImage) convertView.findViewById(R.id.headImage);
            viewHolder.text_name = (TextView) convertView.findViewById(R.id.text_name);
            viewHolder.text_date = (TextView) convertView.findViewById(R.id.text_date);
            viewHolder.photo_layout = (LinearLayout) convertView.findViewById(R.id.photo_layout);
            viewHolder.imageone = (ImageView) convertView.findViewById(R.id.imageone);
            viewHolder.imagetwo = (ImageView) convertView.findViewById(R.id.imagetwo);
            viewHolder.imagethree = (ImageView) convertView.findViewById(R.id.imagethree);
            viewHolder.text_content = (TextView) convertView.findViewById(R.id.text_content);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final CommentBean bean = list.get(position);
//        viewHolder.headImage.setTag(bean.getUserphoto());
        Glide.with(context)
                .load(bean.getUserphoto())
                .placeholder(R.drawable.default_error)
                .error(R.drawable.default_error)
                .centerCrop()
                .crossFade()
                .into(viewHolder.headImage);
//        loadImage.addTask(bean.getUserphoto(), viewHolder.headImage);
//        loadImage.doTask();

        viewHolder.text_name.setText(bean.getNickname());
        viewHolder.text_date.setText(bean.getCreateDate());
        viewHolder.text_content.setText(bean.getContent());

        if (!TextUtils.isEmpty(bean.getPic1url())) {
            viewHolder.photo_layout.setVisibility(View.VISIBLE);
//            viewHolder.imageone.setTag(bean.getPic1url());
            Glide.with(context)
                    .load(bean.getPic1url())
                    .placeholder(R.drawable.default_error)
                    .error(R.drawable.default_error)
                    .centerCrop()
                    .crossFade()
                    .into(viewHolder.imageone);
//            loadImage.addTask(bean.getPic1url(), viewHolder.imageone);
//            loadImage.doTask();

//            viewHolder.imagetwo.setTag(bean.getPic2url());
            Glide.with(context)
                    .load(bean.getPic2url())
                    .placeholder(R.drawable.default_error)
                    .error(R.drawable.default_error)
                    .centerCrop()
                    .crossFade()
                    .into(viewHolder.imagetwo);
//            loadImage.addTask(bean.getPic2url(), viewHolder.imagetwo);
//            loadImage.doTask();

//            viewHolder.imagethree.setTag(bean.getPic3url());
            Glide.with(context)
                    .load(bean.getPic3url())
                    .placeholder(R.drawable.default_error)
                    .error(R.drawable.default_error)
                    .centerCrop()
                    .crossFade()
                    .into(viewHolder.imagethree);
//            loadImage.addTask(bean.getPic3url(), viewHolder.imagethree);
//            loadImage.doTask();
        } else {
            viewHolder.photo_layout.setVisibility(View.GONE);
        }

        viewHolder.imageone.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(bean.getPic1url())) {
                    Intent intent = new Intent(context, PicturePreviewActivity.class);
                    intent.putExtra("url", bean.getPic1url());
                    context.startActivity(intent);
                }
            }
        });

        viewHolder.imagetwo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(bean.getPic2url())) {
                    Intent intent = new Intent(context, PicturePreviewActivity.class);
                    intent.putExtra("url", bean.getPic2url());
                    context.startActivity(intent);
                }
            }
        });

        viewHolder.imagethree.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(bean.getPic3url())) {
                    Intent intent = new Intent(context, PicturePreviewActivity.class);
                    intent.putExtra("url", bean.getPic3url());
                    context.startActivity(intent);
                }
            }
        });

        return convertView;
    }

    class ViewHolder {
        CircularImage headImage;
        TextView text_name;
        TextView text_date;
        LinearLayout photo_layout;
        ImageView imageone;
        ImageView imagetwo;
        ImageView imagethree;
        TextView text_content;
    }
}
